<template>
  <el-form ref="formData" label-position="top" :model="formData" status-icon>
    <el-row :gutter="20">
      <el-col :span="12">
        <el-form-item
          prop="inspectorID"
          :rules="[{ required: true, message: '必须选择巡检人'}]"
          label="巡检人"
        >
          <el-select
            v-model="formData.inspectorID"
            placeholder="请选择巡检人"
            :disabled="isEdit"
          >
            <el-option
              v-for="inspector in allInspector"
              :key="inspector.userId"
              :label="inspector.userName"
              :value="inspector.userId"
            />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item
          prop="damageTypeID0"
          :rules="[{ required: true, message: '必须选择损坏类型'}]"
          label="损坏类型"
        >
          <DamageTypeCascader @valueChanged="id=>{formData.damageTypeID0=id}" />
        </el-form-item>
      </el-col>
    </el-row>
    <el-form-item prop="damageDescription0" :rules="[{ required: true, message: '必须填写损坏描述'}]" label="损坏描述">
      <el-input
        v-model="formData.damageDescription0"
        type="textarea"
        autosize
        placeholder="请输入损坏描述"
      />
    </el-form-item>
    <el-form-item label="备注">
      <el-input
        v-model="formData.remarks0"
        type="textarea"
        autosize
        placeholder="备注"
      />
    </el-form-item>
    <el-row type="flex" justify="end">
      <el-button type="primary" :loading="loading" @click="submit('formData')">提 交</el-button>
    </el-row>
  </el-form>
</template>

<script>
import { getAllUser } from '@/api/user'
import { addDailyInspection, searchDailyInspection, updateDailyInspection } from '@/api/dailyInspection'
import DamageTypeCascader from '@/views/daily-inspection/components/DamageTypeCascader'
import { mapGetters } from 'vuex'
export default {
  name: 'EnrollFrom',
  components: { DamageTypeCascader },
  props: {
    dailyInspectID: {
      type: Number,
      default() {
        return ''
      }
    },
    isEdit: {
      type: Boolean,
      default() {
        return false
      }
    }
  },
  data() {
    return {
      formData: {
        inspectorID: '',
        comNumber: 1,
        damageTypeID0: '',
        damageDescription0: '',
        remarks0: ''
      },
      allInspector: [],
      loading: false
    }
  },
  computed: {
    ...mapGetters([
      'departmentId',
      'userId'
    ])
  },
  created() {
    this.getAllInspector()
    if (this.isEdit) {
      this.getData()
    }
  },
  methods: {
    submit(formName) {
      this.loading = true
      this.$refs[formName].validate((result) => {
        if (result) {
          if (!this.isEdit) {
            this.formData.dailyInspecteID = this.dailyInspectID
            addDailyInspection(this.formData).then(() => {
              this.$emit('success')
              this.$message.success('添加成功')
            })
          } else {
            this.formData.damageTypeID = this.formData.damageTypeID0
            this.formData.remarks = this.formData.remarks0
            this.formData.damageDescription = this.formData.damageDescription0
            updateDailyInspection(this.formData).then(() => {
              this.$emit('success')
              this.$message.success('修改成功')
            })
          }
        } else {
          this.loading = false
          this.$message.error('请检查表单必填项')
          return false
        }
      })
    },
    getAllInspector() {
      if (this.allInspector.length === 0) {
        getAllUser(this.departmentId).then(data => {
          this.allInspector = data
          this.formData.inspectorID = this.userId
        })
      }
    },
    getData() {
      const id = this.dailyInspectID
      searchDailyInspection({ id }).then(data => {
        this.formData.dailyInspecteRecordingComID = data[0].dailyInspecteRecordingComID
        this.formData.inspectorID = data[0].inspectorID.toString()
        this.formData.damageDescription0 = data[0].damageDesciption
        this.formData.remarks0 = data[0].remarks
      })
    }
  }
}
</script>

<style scoped>

</style>
